//
//  LoginViewController.m
//  A_program
//
//  Created by dlios on 15-5-25.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginFirView.h"
#import "LoginFirTableViewCell.h"
#import "CollectListViewController.h"
#import "NovelDatabase.h"
#import "NovelModel.h"
#import "LoginNextViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface LoginViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) LoginFirView *login;
@property (nonatomic, retain) UIButton *loginButton;
- (void)buttonAction:(id)sender;


@end

@implementation LoginViewController
- (void)dealloc
{
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}


- (void)loadView{
    [super loadView];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"我的";
    
}

- (void)buttonAction:(id)sender
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


// 使得登陆后的用户名和头像及时更改
- (void)viewWillAppear:(BOOL)animated
{
    self.login = [[LoginFirView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height / 3)];
    [self.view addSubview:_login];
    
    // tableView
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, self.login.frame.origin.y + _login.frame.size.height, self.view.frame.size.width, 260)];
    table.dataSource = self;
    table.delegate = self;
    table.rowHeight = 70;
    [self.view addSubview:table];
    [table release];
    
    [table registerClass:[LoginFirTableViewCell class] forCellReuseIdentifier:@"a"];
    
}

#pragma mark -
#pragma mark tableView的协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoginFirTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    
    if (indexPath.row == 0){
        
        cell.name.text = @"音乐收藏";
    } else if (indexPath.row == 1) {
        
        cell.name.text = @"电台收藏";
        
    } else if (indexPath.row == 2){
        
        cell.name.text = @"小说收藏";
    }
    
    cell.logo.image = [UIImage imageNamed:@"55.png"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

// cell 跳转下一界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.login.user.text isEqualToString:@"登录"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"还未登录，请先登录" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    } else {
        NSString *str = [NSString string];
        NSUserDefaults *loginName = [NSUserDefaults standardUserDefaults];
        if (indexPath.row == 0) {
            self.mySelectPlayKind = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"music" loginName:[loginName objectForKey:@"loginName"]];
            str = @"music";
        }else if (indexPath.row == 1){
            self.mySelectPlayKind = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"radio" loginName:[loginName objectForKey:@"loginName"]];
            str = @"radio";
        }else if (indexPath.row == 2){
            self.mySelectPlayKind = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"novel" loginName:[loginName objectForKey:@"loginName"]];
            str = @"novel";
        }
        
        CollectListViewController *collect = [[CollectListViewController alloc] init];
        collect.collectArray = self.mySelectPlayKind;
        collect.cancelData = str;
        
        [self.navigationController pushViewController:collect animated:YES];
        [collect release];
        
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
